// @Auth:zdl
package erwang

import (
	"fmt"

	"gitee.com/sky_big/mylog"
	"github.com/gogf/gf/g"
	"github.com/gogf/gf/g/net/ghttp"

	"github.com/zhongdalu/DoorValveErWang/bll"
	"github.com/zhongdalu/DoorValveErWang/public"
	. "github.com/zhongdalu/DoorValveErWang/record"
)

type ErrorCode struct {
	Errno int    `json:"errno"`
	Error string `json:"error"`
}

func Bind(httpUrl, wsbUrl string) {
	public.HttpUrl = httpUrl
	public.WebUrl = wsbUrl
	fmt.Println("使用户阀二网平衡调度系统")
	s := g.Server()
	s.BindHandler("/", func(r *ghttp.Request) {
		r.Response.Writeln("欢迎使用户阀二网平衡调度系统\n版本：V1.0")
	})
	type msg struct {
		Status int `json:"status"`
		ErrorCode
		Ts int64 `json:"ts"`
	}

	s.BindHandler("/erwang", func(r *ghttp.Request) {
		cmd := r.GetInt("cmd")
		var err error
		errCode := msg{}
		switch cmd {
		case -1:
			status := ""
			if Crd != nil && Crd.GetStep() > 0 {
				errCode.Errno = 0
				status = "开启"
				errCode.Status = 1
				errCode.Ts = Crd.GetBeginTs()
			} else {
				errCode.Errno = 0
				status = "关闭"
				errCode.Status = 0
			}
			errCode.Error = "当前状态:" + status
		case 0:
			mylog.Println("关闭二网平衡")
			bll.Close()
			errCode.Errno = 0
			errCode.Error = "关闭成功"
		case 1:
			mylog.Println("开启二网平衡")
			err = bll.InitSetup()
			if err != nil {
				errCode.Errno = -2
				errCode.Error = "开启失败:" + err.Error()
			} else {
				errCode.Errno = 0
				errCode.Error = "开启成功"
			}
		default:
			errCode.Errno = -1
			errCode.Error = "参数错误"
		}
		r.Response.Writeln(errCode)
	})
}
